
def build_home
	
	$entities.clear!	

	$model.definitions.purge_unused

  $main_h_beam_length=(Grid.to_f*8).to_l

	make_home( 4, 2, 4 )
  
  
  
  puts "finish"
  return "OK"

end

def make_home( beams_x, beams_y, floors)
  make_floor ( beams_x, beams_y )

  for f in 0..floors-1
    trans = Geom::Transformation.translation [ 0, 0, f * Floor_height ] 
    $entities.add_instance( $model.definitions['floor'], trans )
  end
end

# make home, parameters are horisontal beam counts
def make_floor ( beams_x, beams_y )
  make_v_beam("v_beam_R4", Floor_height)
  make_floor_main_plate

  componnent = $model.definitions.add('floor') ;  

  # add floor plates
  plates_cnt=0
  for x in 0..($main_h_beam_length*beams_x/Grid)/2-1
    for y in 0..($main_h_beam_length*beams_y/Grid)/4-1
      trans = Geom::Transformation.translation [ x*Grid*2, y*Grid*4, Floor_plate_strength ] 
      componnent.entities.add_instance( $model.definitions['floor_main_plate'], trans )
      plates_cnt += 1
    end
  end
  puts "unsig "+plates_cnt.to_s+" floor plates"
  
  # add four corner beams
  local_correction = Geom::Transformation.translation [-VBeamX/2,-VBeamY/2,0]
  for y in 0..beams_y
      for x in 0..beams_x
        step = Geom::Transformation.translation [x*$main_h_beam_length,y*$main_h_beam_length,0]
        componnent.entities.add_instance( $model.definitions['v_beam_R4'], step * local_correction )
      end
  end
  
  # add horisontal beams in X direction
  length=$main_h_beam_length-VBeamX
  h_beam_name = make_h_beam("h_beam_", length)
  rotation = Geom::Transformation.rotation ORIGIN, [0,1,0], -Math::PI/2
  local_correction = Geom::Transformation.translation [VBeamX/2,-HBeamY/2,-HBeamX]
  local_correction = local_correction*rotation;
  for y in 0..beams_y
      for x in 0..beams_x-1
        step = Geom::Transformation.translation [x*$main_h_beam_length,y*$main_h_beam_length,0]
        componnent.entities.add_instance( $model.definitions[h_beam_name], step * local_correction )
      end
  end

  # add horisontal beams in Y direction
  length=$main_h_beam_length-VBeamY
  h_beam_name = make_h_beam("h_beam_", length)
  rotation_z = Geom::Transformation.rotation ORIGIN, [0,0,1], Math::PI/2
  rotation_y = Geom::Transformation.rotation ORIGIN, [0,1,0], -Math::PI/2
  
  local_correction = Geom::Transformation.translation [HBeamY/2,VBeamY/2,-HBeamX]
  local_correction = local_correction*rotation_z*rotation_y;
  for y in 0..beams_y-1
      for x in 0..beams_x
        step = Geom::Transformation.translation [x*$main_h_beam_length,y*$main_h_beam_length,0]
        componnent.entities.add_instance( $model.definitions[h_beam_name], step * local_correction )
      end
  end

  # add horisontal beams in Y direction bound to other horisontal beams 
  length=$main_h_beam_length-HBeamY
  h_beam_name = make_h_beam("h_beam_", length)
  rotation_z = Geom::Transformation.rotation ORIGIN, [0,0,1], Math::PI/2
  rotation_y = Geom::Transformation.rotation ORIGIN, [0,1,0], -Math::PI/2
  
  local_correction = Geom::Transformation.translation [HBeamY/2,HBeamY/2,-HBeamX]
  local_correction = local_correction*rotation_z*rotation_y;
  for y in 0..beams_y-1
      for x in 0..beams_x-1
        step = Geom::Transformation.translation [x*$main_h_beam_length,y*$main_h_beam_length,0]
        for k in 1..($main_h_beam_length/Grid/2-1).to_i
          step_local = Geom::Transformation.translation [k*Grid*2,0,0]
          componnent.entities.add_instance( $model.definitions[h_beam_name], step * step_local * local_correction )
        end
      end
  end

  # add short horisontal beams in Y direction supporting floor plates
  support_beam_x=HBeamX/2
  support_beam_y=HBeamY/2
  
  length=Grid*2-HBeamY
  h_beam_name = make_h_beam("h_beam_", length, support_beam_x, support_beam_y)
  rotation_y = Geom::Transformation.rotation ORIGIN, [0,1,0], -Math::PI/2
  local_correction = (Geom::Transformation.translation [HBeamY/2, -HBeamY/2,-support_beam_x]) * rotation_y
  for y in 0..beams_y-1
      for x in 0..beams_x-1
        step = Geom::Transformation.translation [x*$main_h_beam_length,y*$main_h_beam_length,0]
        for l in 0..$main_h_beam_length/Grid/2-1
          step_row =  Geom::Transformation.translation [l*Grid*2,0,0]
          for k in 1..($main_h_beam_length/Grid)-1
            step_local = Geom::Transformation.translation [0,k*Grid,0]
            componnent.entities.add_instance( $model.definitions[h_beam_name], step * step_row * step_local * local_correction )
          end
        end
      end
  end
end
